package com.tocici.controller;

import java.util.List;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.stp.StpUtil;
import com.tocici.domain.vo.ArtistVo;
import com.tocici.domain.vo.MusicVo;
import com.tocici.domain.vo.UserVo;
import com.tocici.service.IArtistService;
import com.tocici.service.IMusicService;
import com.tocici.service.IUserService;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import com.tocici.domain.vo.CommentVo;
import com.tocici.domain.bo.CommentBo;
import com.tocici.service.ICommentService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 评论
 *
 * @author zeratul
 * @date 2025-03-02
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/music/comment")
public class CommentController extends BaseController {

    private final ICommentService commentService;

    private final IUserService userService;

    private final IMusicService musicService;

    private final IArtistService artistService;

    /**
     * 查询评论列表
     */
    @SaCheckPermission("music:comment:list")
    @GetMapping("/list")
    public TableDataInfo<CommentVo> list(CommentBo bo, PageQuery pageQuery) {
        return commentService.queryPageList(bo, pageQuery);
    }

    @SaIgnore
    @GetMapping("/list/music/{musicId}")
    public R<List<CommentVo>> listByMusic(@PathVariable Long musicId){
        CommentBo commentBo = new CommentBo();
        commentBo.setMusicId(musicId);
        List<CommentVo> commentVos = commentService.queryList(commentBo);
        commentVos.forEach(commentVo -> {
            UserVo userVo = userService.queryById(commentVo.getUserId());
            commentVo.setUser(userVo);
        });
        return R.ok(commentVos);
    }

    @GetMapping("/list/user")
    @SaIgnore
    public R<List<CommentVo>> listByUser(){
        CommentBo commentBo = new CommentBo();
        String id = StpUtil.getExtra("userId").toString();
        commentBo.setUserId(Long.valueOf(id));
        List<CommentVo> commentVos = commentService.queryList(commentBo);
        commentVos.forEach(commentVo -> {
            MusicVo musicVo = musicService.queryById(commentVo.getMusicId());
            ArtistVo artistVo = artistService.queryById(musicVo.getArtistId());
            musicVo.setArtistVo(artistVo);
            commentVo.setMusic(musicVo);

        });
        System.out.println("commentVos = " + commentVos);
        return R.ok(commentVos);

    }

    /**
     * 导出评论列表
     */
    @SaCheckPermission("music:comment:export")
    @Log(title = "评论", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CommentBo bo, HttpServletResponse response) {
        List<CommentVo> list = commentService.queryList(bo);
        ExcelUtil.exportExcel(list, "评论", CommentVo.class, response);
    }

    /**
     * 获取评论详细信息
     *
     * @param commentId 主键
     */
    @SaCheckPermission("music:comment:query")
    @GetMapping("/{commentId}")
    public R<CommentVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long commentId) {
        return R.ok(commentService.queryById(commentId));
    }

    /**
     * 新增评论
     */
    @SaCheckPermission("music:comment:add")
    @Log(title = "评论", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CommentBo bo) {
        return toAjax(commentService.insertByBo(bo));
    }

    /**
     * 修改评论
     */
    @SaCheckPermission("music:comment:edit")
    @Log(title = "评论", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CommentBo bo) {
        return toAjax(commentService.updateByBo(bo));
    }

    /**
     * 删除评论
     *
     * @param commentIds 主键串
     */
    @SaCheckPermission("music:comment:remove")
    @Log(title = "评论", businessType = BusinessType.DELETE)
    @DeleteMapping("/{commentIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] commentIds) {
        return toAjax(commentService.deleteWithValidByIds(List.of(commentIds), true));
    }

    @PostMapping("/publish")
    @SaIgnore
    public R<Void> publishComment(@RequestBody CommentBo bo){
        String id = StpUtil.getExtra("userId").toString();
        Long userId = Long.valueOf(id);
        bo.setUserId(userId);
        commentService.insertByBo(bo);
        return R.ok();
    }
}
